package com.me.extend.view


import android.util.Size
import android.view.View
import android.view.ViewGroup

/**
 * 测试View的宽高
 * 适用于根据宽高动态布局的地方
 */
fun View.measureView(): Size {
    var params = layoutParams
    if (params == null) {
        params = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
    }
    val widthSpec = ViewGroup.getChildMeasureSpec(0, 0, params.width)
    val height = params.height
    val heightSpec = if (height > 0) {
        View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
    } else {
        View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.UNSPECIFIED)
    }
    measure(widthSpec, heightSpec)
    return Size(measuredWidth, measuredHeight)
}